<?php

require_once APPPATH.'controllers/gnc_proc'.EXT;

class Proc_reminder extends Controller {
	
	//Fungsi untuk mendapatkan tanggal hasil pengurangan
	function find_date_send($jatuhtempo,$selisih_hari) 
	{
		$tstamp = mktime(0,0,0,date("m",strtotime($jatuhtempo)),date("d",strtotime($jatuhtempo)),date("y",strtotime($jatuhtempo)));
		$dstamp = $tstamp-$selisih_hari*60*60*24;
		return date("Y-m-d",$dstamp);
	} 
	function index(){
	echo "proc reminder";
	}
	
	function notify()
	{
		//Query untuk mengambil detail yang akan dikirim secara rutin
		$query_rutin = "select * from rem_t_reminder";
	
		$result_rutin = $this->db->query($query_rutin);
		$result_rutin = $result_rutin->result_array();
		
		foreach($result_rutin as $idx => $val)
		{
			$id= $val['id'];
			$jatuhtempo=$val['dt_jatuh_tempo'];
			$tgl_last_proses=$val['dt_proses'];
			$day_reminder=$val['day_reminder'];
			$rekurensi=$val['rekurensi'];
			
			//pengecekan jatuh tempo
			if($rekurensi == "Tahunan") {
				
				$d_send= $this->get_jth_tempo_tahunan ($jatuhtempo,$day_reminder, $tgl_last_proses);
	
			}elseif($rekurensi == "Bulanan") {
				$d_send = $this->get_jth_tempo_bulanan ($jatuhtempo,$day_reminder, $tgl_last_proses);				
			}elseif($rekurensi == "Mingguan") {
				$d_send = $this->get_jth_tempo_mingguan ($jatuhtempo,$day_reminder, $tgl_last_proses);				
			}
			elseif($rekurensi == "Harian") {
				$d_send = $this->get_jth_tempo_harian ($jatuhtempo,$day_reminder, $tgl_last_proses);
			}
			else{
				echo "<br>sekali<br>";
				$d_send = $this->get_jth_tempo_sekali ($jatuhtempo,$day_reminder,$tgl_last_proses);
				
			}
			echo "$jatuhtempo $rekurensi ## $d  ##<br>";
		
			//jika d_send --> perlu di send
			if($d_send) {	
				//push to outbox
				$data = array(
					'from' => $val['email_penanggung_jawab'],
					'to' => $val['email_to'],
					'cc' => $val['email_cc'],
					'subjek' => "[".$val['bidang']."]".$val['deskripsi'],
					'body' => $val['pesan']
				);
				$this->db->insert('rem_t_outbox', $data); 

				//update status
				$data = array(
				   'dt_proses' => date("Y-m-d H:i:s")
				 );
				$this->db->where('id', $val['id']);
				$this->db->update('rem_t_reminder', $data);
				echo "<b> dikirim <br>";
			}
			else{echo "<b> tidak dikirim <br>";}
			
		}
		
	}

	function get_jth_tempo_tahunan ($jatuhtempo,$day_reminder, $tgl_last_proses) {
		//jika tahunan abaikan tahun jatuhtempo dan ganti dengan tahun sekarang
		//format jatuhtempo yyyy-mm-dd
        echo "<b> TAHUNAN <br></b> ";
		$today = time();

		$cur_jatuhtempo = substr_replace($jatuhtempo, date("Y"), 0, 4);
		
		echo "<b> $cur_jatuhtempo</b> ";

		$cur_rem = strtotime($cur_jatuhtempo) - $day_reminder*24*60*60;
	    $f=(strtotime($cur_jatuhtempo)+24*60*60)-1;
		echo " #cur $cur_rem ".date("Y-m-d",$cur_rem)." cjt ".$f."# <br>";
		$a = strtotime(date("Y-m-d",$cur_rem));
		$b = strtotime($tgl_last_proses);
		$c =strtotime($cur_jatuhtempo);
		echo "$a >= $b dan $today > = $cur_rem dan $today <= $c<br>";
		if($cur_rem >= strtotime($tgl_last_proses) and $today >= $cur_rem and $today <= ($cur_rem + 24*60*60)-1) {
			//belum diproses tahun ini
			return date("Y-m-d",$cur_rem);
		}else{
			return null;
		}		
	}

	function get_jth_tempo_bulanan($jatuhtempo,$day_reminder, $tgl_last_proses) {
		echo "<b> BULANAN <br></b> ";
		//jika bulanan abaikan tahun dan bulan jatuhtempo dan ganti dengan 
		//tahun dan bulan sekarang
		//format jatuhtempo yyyy-mm-dd
		$today = time();
		
		//penanganan khusus untuk menangani jumlah hari yang tidak sama pada setiap bulan
		$cur_jatuhtempo = substr_replace($jatuhtempo, date("Y-m"), 0, 7);
		$cur_jatuhtempo = substr_replace($cur_jatuhtempo, "00", 8, 2);
		$cur_jatuhtempo = date("Y-m-d",strtotime($cur_jatuhtempo) + substr($jatuhtempo,8,2)*24*60*60);
		
		$cur_rem = strtotime($cur_jatuhtempo) - $day_reminder*24*60*60;

		echo " baru# ".date("Y-m-d",$cur_rem)." # ";
		//and $today >= $cur_rem and $today <= $jatuhtempo
		$a = strtotime(date("Y-m-d",$cur_rem));
		$b = strtotime($tgl_last_proses);
		$c =strtotime($cur_jatuhtempo);
		echo "$a >= $b dan $today > = $cur_rem dan $today <= $c<br>";
		if($cur_rem >= strtotime($tgl_last_proses) and $today >= $cur_rem and $today <= ($cur_rem + 24*60*60)-1) {
			//belum diproses bulan ini
			return date("Y-m-d",$cur_rem);
		}else{
			return null;
		}		

	}
	
	function get_jth_tempo_mingguan($jatuhtempo,$day_reminder, $tgl_last_proses) {
		echo "<b> Mingguan <br></b> ";
		//format jatuhtempo yyyy-mm-dd
		$today = time();
		if ($tgl_last_proses != 0)
			$cur_jatuhtempo=date("Y-m-d",strtotime($tgl_last_proses)+8*24*60*60);
		else
			$cur_jatuhtempo = substr_replace($jatuhtempo, date("Y"), 0, 4);
		
		$cur_rem = strtotime($cur_jatuhtempo)- $day_reminder*24*60*60;
		//and $today >= $cur_rem and $today <= $jatuhtempo
		if($cur_rem >= strtotime($tgl_last_proses) and $today >= $cur_rem and $today < strtotime($cur_jatuhtempo)) {
			//belum diproses bulan ini
			return date("Y-m-d",$cur_rem);
		}else{
			return null;
		}		

	}
	
	function get_jth_tempo_harian ($jatuhtempo,$day_reminder,$tgl_last_proses) {
	    echo "<b> HARIAN <br></b> ";
		$today = time();
		$b=$jatuhtempo;
		$d=date("Y-m-d",$today);
		echo "harian $b fg $d<br>";		
		if ($tgl_last_proses == null or $tgl_last_proses == 0){
			echo "masih null";
			$cur_rem = strtotime($jatuhtempo) - $day_reminder*24*60*60;	
		}
		else{
			$cur_rem = strtotime($tgl_last_proses) + 24*60*60;
		}
		$a = strtotime(date("Y-m-d",$cur_rem));
		
		$c = (strtotime($jatuhtempo)+24*60*60)-1;
		echo "$today >= $a dan $today <= $c  tgl $b akhir <br>";
		if($today >= $cur_rem and $today <= (strtotime($jatuhtempo)+24*60*60)-1) {
				//belum diproses
				return date("Y-m-d",$cur_rem);
		}else{
				return null;
		}
	}
	
	
	function get_jth_tempo_sekali ($jatuhtempo,$day_reminder,$tgl_last_proses) {
	    echo "<b> SEKALI <br></b> ";
		$today = time();
			
		$cur_rem = strtotime($jatuhtempo) - $day_reminder*24*60*60;
		echo "cur_rem ".date("Y-m-d",$cur_rem)." <br>";
		if($tgl_last_proses == null and $today >= $cur_rem and $today <= ($cur_rem + 24*60*60)-1) {
			//belum diproses
			return date("Y-m-d",$cur_rem);
		}else{
			return null;
		}	
	}
	

}
?>